/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import business.UserBusiness;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import model.Admin;
import model.Customer;

/**
 *
 * @author Administrator
 */
@ManagedBean
@SessionScoped
public class LoginBean {

    private UserBusiness userBusiness = new UserBusiness();
    private Admin admin;
    private Customer customer;

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public Admin getAdmin() {
        return admin;
    }

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }

    public String loginAdminAction() {
        if (userBusiness.loginAdmin(admin) != null) {
            HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
            session.setAttribute("admin", userBusiness.loginAdmin(admin));
            return "templateadmin.xhtml";
        }
        return "login.xhtml";
    }

    public String loginCustomerAction() {
        if (userBusiness.loginCustomer(customer) != null) {
            HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
            session.setAttribute("customer", userBusiness.loginCustomer(customer));
            return "index.xhtml";
        }
        return "index.xhtml";
    }

    public String logoutAdminAction() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        session.setAttribute("admin", null);
        return "login.xhtml";
    }

    public String logoutCustomerAction() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        session.setAttribute("customer",null);
        return "index.xhtml";
    }

    /** Creates a new instance of LoginBean */
    public LoginBean() {
        admin = new Admin();
        customer = new Customer();
    }
}
